package com.ydev.workouthelper.gui;

import com.ydev.workouthelper.R;
import com.ydev.workouthelper.R.layout;
import com.ydev.workouthelper.R.menu;

import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class Workout extends Fragment  {

	Activity a;
	
	public View v;
	
	Button btnStartSet;
	
	EditText editTextDesc;
	TextView tv_weight;
	TextView tv_reps;
	TextView tv_sets;
	TextView tv_between;
	
	LinearLayout llNumberPicker1;
	SeekBar sb11;
	SeekBar sb12;
	SeekBar sb13;
	Button btnDone1;
	
	LinearLayout llNumberPicker2;
	SeekBar sb21;
	SeekBar sb22;
	SeekBar sb23;
	Button btnDone2;
	
	LinearLayout llNumberPicker3;
	SeekBar sb31;
	SeekBar sb32;
	SeekBar sb33;
	Button btnDone3;
	
	LinearLayout llNumberPicker4;
	SeekBar sb41;
	SeekBar sb42;
	SeekBar sb43;
	Button btnDone4;
	
	private long startTime = 0L;

	private Handler customHandler = new Handler();

	long timeInMilliseconds = 0L;
	long timeSwapBuff = 0L;
	long updatedTime = 0L;
	 long countdownFrom;

	
	 /** (non-Javadoc)
    * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
    */
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
           Bundle savedInstanceState) {
       if (container == null) {
           // We have different layouts, and in one of them this
           // fragment's containing frame doesn't exist.  The fragment
           // may still be created from its saved state, but there is
           // no reason to try to create its view hierarchy because it
           // won't be displayed.  Note this is not needed -- we could
           // just run the code below, where we would create and return
           // the view hierarchy; it would just never be used.
           return null;
       }       
       v= (LinearLayout)inflater.inflate(R.layout.workout_layout, container, false);
       
       init();
       
       return v;
   }
   
   public void init(){
	   a =getActivity();
	   
	   btnStartSet=(Button)v.findViewById(R.id.btnStartSet);
	   
	   editTextDesc=(EditText)v.findViewById(R.id.editTextDesc);
	   tv_weight=(TextView)v.findViewById(R.id.textViewWeight);
	   tv_reps=(TextView)v.findViewById(R.id.tv_reps);
	   tv_sets=(TextView)v.findViewById(R.id.tv_sets);
	   tv_between=(TextView)v.findViewById(R.id.tv_between);
	   
	   llNumberPicker1=(LinearLayout)v.findViewById(R.id.llNumberPicker1);
	   sb11=(SeekBar)v.findViewById(R.id.sb11);
	   sb12=(SeekBar)v.findViewById(R.id.sb12);
	   sb13=(SeekBar)v.findViewById(R.id.sb13);
	   btnDone1=(Button)v.findViewById(R.id.buttonDone1);
	   
	   llNumberPicker2=(LinearLayout)v.findViewById(R.id.llNumberPicker2);
	   sb21=(SeekBar)v.findViewById(R.id.sb21);
	   sb22=(SeekBar)v.findViewById(R.id.sb22);
	   sb23=(SeekBar)v.findViewById(R.id.sb23);
	   btnDone2=(Button)v.findViewById(R.id.ButtonDone2);
	   
	   llNumberPicker3=(LinearLayout)v.findViewById(R.id.llNumberPicker3);
	   sb31=(SeekBar)v.findViewById(R.id.sb31);
	   sb32=(SeekBar)v.findViewById(R.id.sb32);
	   sb33=(SeekBar)v.findViewById(R.id.sb33);
	   btnDone3=(Button)v.findViewById(R.id.ButtonDone3);
	   
	   llNumberPicker4=(LinearLayout)v.findViewById(R.id.llNumberPicker4);
	   sb41=(SeekBar)v.findViewById(R.id.sb41);
	   sb42=(SeekBar)v.findViewById(R.id.sb42);
	   sb43=(SeekBar)v.findViewById(R.id.sb43);
	   btnDone4=(Button)v.findViewById(R.id.ButtonDone4);
	   
	   OnSeekBarChangeListener sl = new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
			//	int id = seekBar.getId();
			//	String name=a.getResources().getResourceEntryName(id);
				
				int sum1=sb11.getProgress() * 100 + sb12.getProgress()*10 + sb13.getProgress();
				tv_weight.setText(sum1+"");
				
				int sum2=sb21.getProgress() * 100 + sb22.getProgress()*10 + sb23.getProgress();
				tv_reps.setText(sum2+"");
				
				int sum3=sb31.getProgress() * 100 + sb32.getProgress()*10 + sb33.getProgress();
				tv_sets.setText(sum3+"");
				
				int sum4=sb41.getProgress() * 100 + sb42.getProgress()*10 + sb43.getProgress();
				tv_between.setText(sum4+"");
			}
		};
	   
	   sb11.setOnSeekBarChangeListener(sl);
	   sb12.setOnSeekBarChangeListener(sl);
	   sb13.setOnSeekBarChangeListener(sl);
	   
	   sb21.setOnSeekBarChangeListener(sl);
	   sb22.setOnSeekBarChangeListener(sl);
	   sb23.setOnSeekBarChangeListener(sl);
	   
	   sb31.setOnSeekBarChangeListener(sl);
	   sb32.setOnSeekBarChangeListener(sl);
	   sb33.setOnSeekBarChangeListener(sl);
	   
	   sb41.setOnSeekBarChangeListener(sl);
	   sb42.setOnSeekBarChangeListener(sl);
	   sb43.setOnSeekBarChangeListener(sl);
	   
	   OnClickListener cl= new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			llNumberPicker1.setVisibility(View.GONE);
			llNumberPicker2.setVisibility(View.GONE);
			llNumberPicker3.setVisibility(View.GONE);
			llNumberPicker4.setVisibility(View.GONE);
			
		}
	};
	   
	   btnDone1.setOnClickListener(cl);
	   btnDone2.setOnClickListener(cl);
	   btnDone3.setOnClickListener(cl);
	   btnDone4.setOnClickListener(cl);
	  
	   a.getWindow().setSoftInputMode(
			      WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
	   InputMethodManager mgr = (InputMethodManager) a.getSystemService(Context.INPUT_METHOD_SERVICE);
	   mgr.hideSoftInputFromWindow(editTextDesc.getWindowToken(), 0);
	   
	   editTextDesc.setOnFocusChangeListener(new OnFocusChangeListener() {
		
		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			 Activity a =getActivity();
			 InputMethodManager mgr = (InputMethodManager) a.getSystemService(Context.INPUT_METHOD_SERVICE);
			   mgr.hideSoftInputFromWindow(editTextDesc.getWindowToken(), 0);
			
		}
	});
	   
	   tv_weight.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			llNumberPicker1.setVisibility(View.VISIBLE);
			llNumberPicker2.setVisibility(View.GONE);
			llNumberPicker3.setVisibility(View.GONE);
			llNumberPicker4.setVisibility(View.GONE);
			
		}
	});
	   
	   tv_reps.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				llNumberPicker2.setVisibility(View.VISIBLE);
				llNumberPicker1.setVisibility(View.GONE);
				llNumberPicker3.setVisibility(View.GONE);
				llNumberPicker4.setVisibility(View.GONE);
				
			}
		});
	   
	   tv_sets.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				llNumberPicker3.setVisibility(View.VISIBLE);
				llNumberPicker2.setVisibility(View.GONE);
				llNumberPicker1.setVisibility(View.GONE);
				llNumberPicker4.setVisibility(View.GONE);
				
			}
		});
	   
	   tv_between.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				llNumberPicker4.setVisibility(View.VISIBLE);
				llNumberPicker2.setVisibility(View.GONE);
				llNumberPicker3.setVisibility(View.GONE);
				llNumberPicker1.setVisibility(View.GONE);
				
			}
		});
	   
	   btnStartSet.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			startTime = SystemClock.uptimeMillis();
			countdownFrom = sb41.getProgress() * 100 + sb42.getProgress()*10 + sb43.getProgress();
			customHandler.postDelayed(updateTimerThread, 0);
			
		}
	});
	   
	   
   }
  
   private Runnable updateTimerThread = new Runnable() {
		public void run() {

			timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
			
			updatedTime = countdownFrom*1000-(timeSwapBuff + timeInMilliseconds);

			int secs = (int) (updatedTime / 1000);
			int mins = secs / 60;
			secs = secs % 60;
			int milliseconds = (int) (updatedTime % 1000);
			tv_between.setText("" + mins + ":"
					+ String.format("%02d", secs) + ":"
					+ String.format("%03d", milliseconds));
			if(updatedTime>=0)
				customHandler.postDelayed(this, 0);
			else{
				reachedZero();
				
			}
		}

		private void reachedZero() {
			int sum3=sb31.getProgress() * 100 + sb32.getProgress()*10 + sb33.getProgress();
			tv_sets.setText(sum3+1+"");
			
			tv_between.setText(countdownFrom + "");
			customHandler.removeCallbacks(updateTimerThread);
			
		}

	};
}
